<?php
//列表控制器类
class ListController extends CommonController{
    private $model;
    /**
     * [__init 构造函数]
     * @return [type] [description]
     */
    public function __init(){
        parent::__init();
        $this->model=K("Category");
    }
    //动作方法
    public function index(){
        // 分类栏目数据
        $cateData = $this->model->get_data();
        $cateD =$cateData;
        $arr = array();
        foreach ($cateData as $k => $v) {
            if ($v['pid']==0) {
                $arr[$k]= $v;
                $arr[$k]["son"]=$this->get_son($cateData,$v['cid']);
            }
        }
        $cateData = $arr;
        $this->assign('cateData',$cateData);
        // 分类产品数据获取
        $cid  = Q('get.cid',0,"intval");
        // 判断分类传参数是否符合
        if($cid<1){
            $this->success('没有当前分类',__ROOT__);
        }
        $cateInfo =$this->model->get_one_data(array('cid'=>$cid));
        $this->assign('cateInfo',$cateInfo);
        // 获取父级分类下的子分类的
        $sonData=$this->get_son($cateD,$cid);
        // p($sonData);
        $son ="";
        foreach ($sonData as $v) {
            $son .=$v['cid'].",";  
        }
        $son=rtrim($son,",");
        if (empty($sonData)) {
            $son=$cid;
        }
        // p($son);
        // 获取所有分类下的商品ID
        $where ='cid in('.$son.')';
        // p($where);
        $model =K('Goods');
        $gids = $model->get_gids($where,"gid");
        // p($gids);
        // 组合查询条件
        if (!empty($gids)){
            $where ='gid in('.implode(',', $gids).')';
        }else{
            // $where = 'gid in(0)';
            $this->success('当前分类下目前没有产品,请浏览其它分类',__ROOT__);
        }
        //老师所讲*************************
        // $cid = Q('get.cid', 0, 'intval');
        // //获得分类下面的所有商品id
        // $gidArr = K('Goods')->where(array('cid'=>$cid))->getField('gid',true);
        // $gidStr = implode(',', $gidArr);
        //获得商品所对应的属性
        $attr = K('GoodsAttr')->where($where)->group('gtvalue')->all();
        // p($attr);

        $tempOne = array();
        foreach ($attr as $k => $v) {
            $tempOne[$v['taid']][] = $v;
        }
        // p($tempOne);die;
        $tempTwo = array();
        $typeAttrModel = K('TypeAttr');
        foreach ($tempOne as $k => $v) {
            $tempTwo[] = array(
                'tname' => $typeAttrModel->where(array('taid'=>$k))->getField('taname'),
                'option' => $v
                );
        }
        $this->assign('typeAttr',$tempTwo);
        $num = count($tempTwo);
        $param = isset($_GET['param']) ? explode('_', $_GET['param']) : array_fill(0, $num, 0);
        $this->assign('param',$param);
        // p($param);
        //老师所讲*************************
        $goodsId = array();
        foreach ($param as $v)
        {
            if ($v > 0)
            {
                $sql = 'SELECT b.gid FROM dnshop_goods_attr as a JOIN dnshop_goods_attr as b ON a.gtvalue= b.gtvalue WHERE a.gaid =' . $v;
                $gidData =K('GoodsAttr')->query($sql);
                // p($gidData);
                $gidArr = array();
                foreach ($gidData as $v) {
                   $gidArr[]=$v['gid']; 
                }
                $goodsId[] = $gidArr;

            }
        }
        // p($goodsId);
        if(!empty($goodsId)){
            $gidsArr = $gids;
            foreach ($goodsId as $v) {
                $gidsArr= array_intersect($gidsArr,$v);
            }
            if (!empty($gidsArr)) {
                $where ='gid in('.implode(',', $gidsArr).')';
            }else{
                $where ='gid in(0)';
            }
        }
        $order ="time desc";
        // 判断是否有点击最新
        if (isset($_GET['new'])){
            if(Q('get.new',0,("intval"))){
                $order="time desc";
            };
        }
        // 判断是否有点击价格排序
        if (isset($_GET['price'])){
            if(Q('get.price',0,("intval"))==1){
                $order="shopprice desc";
            }else{
                $order="shopprice asc";
            }
        }
        // 判断是否有点击关注度排序
        if (isset($_GET['click'])){
            if(Q('get.click',0,("intval"))){
                $order="click desc";
            }
        }
        //// 分配产品数据
        $listData = $model->get_data($where);
        $total=count($listData);
        $pageObj= new Page($total,12,3);
        $page = $pageObj->show();
        $this->assign("page",$page);
        $listData = $model->get_data($where,$pageObj->limit(),$order);
        // p($listData);
        $this->assign('listData',$listData);
        //显示视图
        $this->dis("list.html");
    }



}
